/*******************************************************************
 * File:     interact
 * Purpose:  Interactive interface
 * Author:   Justin Fletcher
 * Date:     11 Jan 2006
 * License:  Free for any use with no restrictions or requirements
 ******************************************************************/

#ifndef INTERACT_H
#define INTERACT_H

/* Each command we can process has the following information
   associated with it : */
typedef struct interactcommand_s {
  const char *command; /* The command name we match, or NULL for
                          end of list */
  const char *help;    /* Any help text for this command */

  /* A function which can be used to process the command when it
     is given. Return value will be passed back to caller. For
     the shell, any non-zero value terminates.
     The tail string may be written to, but writing should be
     limited to the extent of the tail string. */
  int (*process_func)(char *tail);
} interactcommand_t;

/* To link the structures together we use a linker-set. This is
   simply a linked list of interactlink_t structures, which we
   can process. The order of these structures will be
   indeterminate at run time. If we wanted to, we could order
   them as being sorted in some manner on initialisation. However,
   for our purposes we do not care. */
typedef struct interactlink_s interactlink_t;
struct interactlink_s {
  interactlink_t *next;
  interactcommand_t *commands;
};

/*************************************************** Gerph *********
 Function:     interact_command
 Description:  Process a command for our interaction interface
 Parameters:   cli-> the command line to process
               invalid_terminates = the return code for an invalid
                                    command
 Returns:      return code for the command - non-zero will terminate
               the interaction shell, remember
 Note:         cli is intentionally NOT const. It will be written to
               to break down the command and tail. Writing will not
               exceed the extent of the string.
 ******************************************************************/
int interact_command(char *cli, int invalid_rc);

/*************************************************** Gerph *********
 Function:     interact_shell
 Description:  Run the interaction interface
 Parameters:   prompt-> the prompt to give
 Returns:      value returned from final interaction command,
               or 0 if the last command returned no error
 ******************************************************************/
int interact_shell(const char *prompt);

#endif
